﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Actuals.CoreDomain.Business;
using Actuals.Business;
using Actuals.CoreDomain;

namespace Actuals
{
    public partial class AddTeamMembers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (Session["EmployeeId"] == null)
            //{
            //    Response.Write("Please login!");
            //    Response.End();
            //}
            if (!Page.IsPostBack)
            {
                IBusinessEmployee businessEmployee = new BusinessEmployee();

                IList<IEmployee> employees = businessEmployee.GetEmployees();

                ddlEmployee.DataSource = employees;
                ddlEmployee.DataTextField = "NickName"; //string.Format("{0}-{1)" ,"FirstName","LastName");
                ddlEmployee.DataValueField = "Id";
                ddlEmployee.DataBind();


                IBusinessTeam businessTeam = new BusinessTeam();
                IList<ITeam> teams = businessTeam.GetTeams();
                ddlTeam.DataSource = teams;
                ddlTeam.DataTextField = "TeamDescription";
                ddlTeam.DataValueField = "TeamId";
                ddlTeam.DataBind();

                string _teamId = Request.QueryString["TeamId"].ToString();
                ViewState["TeamId"] = _teamId;

                if (Request.QueryString["Mode"] == null)
                {
                    ddlTeam.SelectedValue = _teamId.ToString();
                    ddlTeam.Enabled = false;
 
                }
                else
                {
                    string _teamMemberId = Request.QueryString["TeamMemberId"].ToString();
                    ViewState["TeamMemberId"] = _teamMemberId;
                    ViewState["Mode"] = Request.QueryString["Mode"];
                    ddlTeam.Enabled = true;
                    PopulateTeamMember(Int64.Parse(_teamMemberId));
                }


            }



        }

        protected void PopulateTeamMember(Int64 teamMemberId)
        {
            IBusinessTeamMember businessTeamMember = new BusinessTeamMember();
            ITeamMember teamMember = businessTeamMember.GetTeamMember(teamMemberId);

            ddlEmployee.SelectedValue = teamMember.EmployeeId.ToString();
            txtNoOfHoursPerDay.Text = teamMember.NoOfHoursPerDay.ToString();
            ddlTeam.SelectedValue = teamMember.TeamId.ToString();


        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                ITeamMember teamMember = new TeamMember();
                 teamMember.TeamId = int.Parse( ViewState["TeamId"].ToString());
                teamMember.WorkMode = ddlWorkMode.SelectedValue;
                teamMember.EmployeeId = int.Parse(ddlEmployee.SelectedValue);
                teamMember.NoOfHoursPerDay = int.Parse(txtNoOfHoursPerDay.Text);
                IBusinessTeamMember businessTeamMember = new BusinessTeamMember();
                if (ViewState["Mode"] != null)
                {
                    teamMember.Id = Int64.Parse(ViewState["TeamMemberId"].ToString());
                    businessTeamMember.UpdateTeamMember(teamMember);
                }
                else
                    businessTeamMember.AddTeamMember(teamMember);
                lblMsg.Text = "Saved";
                
            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.Message;
            }
        }
         

















               }
        }

